package ops.model.manager;

import java.io.File;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;

import ops.model.Class;
import ops.model.Student;

public class StudentManager extends EntityManager<Student>{

	private static final File STUDENTS_FILE = new File("data/students.dat");
	
	public StudentManager() {
		super();
	}
	
	@Override
	public File getFile() {
		return STUDENTS_FILE;
	}

	@Override
	protected Object getKeyForEntity(Student element) {
		return (Long) element.getCode();
	}
	
	@Override
	public Student remove(Object key) {
		// removing the student from all the classes he belong to
		Map<Object, Class> classes = EntityController.getInstance().getClassManager().getModifiableMap();
		for (Class clazz : classes.values()) {
			for (Iterator<Entry<Long, Double>> iter = clazz.getStudentsGrade().entrySet().iterator(); iter.hasNext();) {
				if (iter.next().getKey() == key) {
					iter.remove();
					break; // no need to iterate anymore since the key is unique!
				}
			}
		}
		
		//students.remove(studentCode);
		
		return super.remove(key);
	}

}
